package com.kgc.controller;

import com.kgc.bean.GoodsItem;
import com.kgc.bean.GoodsItemVTO;
import com.kgc.bean.User;
import com.kgc.config.annotation.RequestPermission;
import com.kgc.config.annotation.RequestUser;
import com.kgc.service.ArrivalRemindService;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created on 2021/6/22.
 * Author: lzy
 * Description: 用户设置到货提醒入口
 */
@Api(tags = "设置到货提醒入口")
@RestController
@Slf4j
public class ArrivalRemindController {


    @Autowired
    private ArrivalRemindService arrivalRemindService;

    /**
     * @ClassName: ArrivalRemindController
     * @Description 用户登录后设置无货商品的到货提醒
     * @Author lzy
     * @Date 2021/6/23
     */
    @PostMapping("/addArrival")
    @ApiOperation(value = "用户设置到货提醒", notes = "需要传入商品id,需要用户输入手机号")
    @ApiResponses({
            @ApiResponse(code = 6999, message = "设置到货提醒失败"),
            @ApiResponse(code = 0000, message = "设置到货提醒成功")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", required = true, value = "商品编号")
            , @ApiImplicitParam(name = "phoneNo", required = false, value = "客户设置手机号")})
    @RequestPermission
    public ReturnResult<String> addArrivalRemin(@RequestParam String goodsId,
                                                @RequestParam(required = false) String phoneNo,
                                                @ApiIgnore @RequestUser User user) {
        // 先判断用户有没有自己输入手机号
        if (null == phoneNo || phoneNo == "") {
            // 如果没有输入，就去用户中心查询用户的手机号
            if (null != user.getPhone()) {
                phoneNo = user.getPhone();
                // log.info("获取的用户手机号为：{}，是否为空：{}",user.getPhone(), StringUtils.isEmpty(user.getPhone()));
            } else {
                // 查询不到手机号，返回用户未设置手机号
                return ResultBuildUtil.fail("6998", "用户手机号为空");
            }
        }
        // 判断该用户是否设置到货提醒
        log.info("------用户是否添加过了：{}", arrivalRemindService.judgeSetArrivalRemind(goodsId, phoneNo));
        if (arrivalRemindService.judgeSetArrivalRemind(goodsId, phoneNo)) {
            return ResultBuildUtil.fail("6997", "用户已添加提醒，不可重复添加");
        } else {
            arrivalRemindService.addArrivalRemind(goodsId, phoneNo);
            // 存入缓存中，返回用户设置提醒成功
            return ResultBuildUtil.success("设置到货提醒成功");
        }
    }


    /**
     * @ClassName: ArrivalRemindController
     * @Description 根据商品id显示商品详情
     * @Author lzy
     * @Date 2021/6/29
     */
    @GetMapping("/showGoodsInfoById")
    @ApiOperation(value = "通过商品编号,来展示具体商品详情", notes = "展示所有商品详情")
    @ApiResponses({
            @ApiResponse(code = 0000, message = "返回成功"),
            @ApiResponse(code = 9006, message = "对不起，你查询的该商品不存在")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", required = true, value = "商品编号")
    })
    public ReturnResult<GoodsItemVTO> showGoodsInfoByGoodsId(@RequestParam String goodsId) {

        // 先判断是否存在该商品
        if (ObjectUtils.isEmpty(arrivalRemindService.queryselectGoodsByGoodsId(goodsId))) {
            return ResultBuildUtil.fail("9006", "对不起，你查询的该商品不存在");
        }
        GoodsItemVTO goodsItemVTO = new GoodsItemVTO();
        BeanUtils.copyProperties(arrivalRemindService.queryselectGoodsByGoodsId(goodsId), goodsItemVTO);

        // 存在查询出商品的详情
        return ResultBuildUtil.success(goodsItemVTO);
    }


}
